<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/user.jsp"%>
<%
/**
**判断下医院1113的才调用 本地库所有查询加医院1113条件限制 status为1 写病人基本信息
*/
    Integer uid = token.getUid();
	User user = userByUid(uid);
	if (isEmpty(user)) {
		msg = new Message(30041, "用户不存在");
        Tools.encryptPrint(out, msg);
        return;
	}
	if (!USER_PATIENT.equals(user.getType())) {
		msg = new Message(30041, "您暂时无法进行实名认证");
        Tools.encryptPrint(out, msg);
        return;
	}
	String name = input.getDesc("name", "");
	String regexStr = "^[\u4e00-\u9fa5]+(·[\u4e00-\u9fa5]+)*$";
	if (!name.matches(regexStr)) {
		Tools.encryptPrint(out, new Message(30041, "姓名只能为中文汉字"));
		return ;
	}
	if (name.length() > 10 || name.length() < 2 ) {
		Tools.encryptPrint(out, new Message(30041, "姓名为2-10位汉字"));
		return ;
	}
	String cardNumber = input.getDesc("card_number", "");
	cardNumber = cardNumber.toUpperCase();
	String cardType = input.getDesc("card_type", "身份证");
	UserProfile userProfile = selfProfileByUid(uid);
	if (userProfile != null) {
		userProfile.setCardType(cardType);
		userProfile.setName(name);
		userProfile.setCardNumber(cardNumber);
		cardNumber = fixPersonIDCode(cardNumber);
		if ("".equals(userProfile.getCardType())) 
			userProfile.setCardType("身份证");
		if (userProfile.getCardType().contains("身份证")) {
			// 校验身份证号码合法性
			if (!"".equals(cardNumber)) {
				if(!IdCardUtil.validateIdCard18(cardNumber)){
					Tools.encryptPrint(out, new Message(30028, "证件号码不正确"));
					return ;
				}
				user.setBirthday(IdCardUtil.getBirth(cardNumber));
				user.setGender((IdCardUtil.getGender(cardNumber))%2);
				userProfile.setGender(user.getGender());
				userProfile.setBirthday(user.getBirthday());
				userProfile.setAge(IdCardUtil.getAgeByIdCard(cardNumber));
				userProfile.setCardType("身份证");
			}
		}
		List userProfiles = QBuilder.db("basedb").select().from("UserProfile").where(new Where("hospital=", hospital).and("status = ", STATUS_ENABLE).and("cardNumber=", cardNumber).and("self=", SELF).and("uid!=", user.getId())).query().getResultObj();
		if (userProfiles.size() > 0) {
			Tools.encryptPrint(out, new Message(30042, "该身份证已被绑定了账户,如需申诉请联系我们"));
			return ;
		}
		DBTools.db("basedb").modItem(userProfile, userProfile.getId());
	}
	user.setName(name);
	DBTools.db("basedb").modItem(user, user.getId());
	
	
	// 调用realnamejudge查询是否有记录
	String rnUrl = HOST_BASE_URL_1113 + "/appoint/realnamejudge.jsp";
	Map<String, String> rnParam  = new HashMap<String,String>(2);
	rnParam.put("cardNumber",cardNumber);
	String rnRet = HttpUtil.post(rnUrl,rnParam);
    Map<Object, Object> map =  JSONUtil.mapFromJson(rnRet);
    int myErrCode = (Integer)map.get("errcode");
	
	// add by liuzq  医院1113  写病人基本信息
	if(hospital == 1113 && myErrCode==30041) { // 30041表示没有记录
		/**
		**增加病人信息 
		**/
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		int seed = new Random().nextInt(999999);
		seed = seed < 100000 ? (seed += 100000): seed;
		String sickName          =  name;     // 姓名        
		String cardNum        =  String.valueOf(sdf.format(new Date())+seed);   //卡号 
		String sfzh          =  cardNumber; // 身份证号
		String sex           =  IdCardUtil.getGender(sfzh)%2==0?"女":"男";      //性别  
		String birth         =  sfzh.substring(6, 14);    // 出生日期 
		String lxdz          =  "暂无";     //联系地址（必填）
		String lxdh 		 =	userProfile.getPhone(); //联系电话（必填）  

		//写病人基本信息
		Map<String, String> param  = new HashMap<String,String>(8);
		
		param.put("sickName",sickName);                        
		param.put("cardNum",cardNum);                             
		param.put("sfzh",sfzh);                 
		param.put("sex",sex); 
		param.put("birth",birth);               
		param.put("lxdz",lxdz);                 
		param.put("lxdh",lxdh);                 
	    
	    String url = HOST_BASE_URL_1113 + "/appoint/addpatientinfo.jsp";
	    String ret = HttpUtil.post(url,param);
	    System.out.println("20180605real_name:"+param);
	    msg.put("msg",ret);
	}
	
    Tools.encryptPrint(out, msg);
%>
